<?php

namespace App\Http\Requests;

use App\Util\ApiResult;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;

class LoginApiRequest extends FormRequest
{

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        $this->needsJson();
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'tel' => ['bail','required','regex:/^1[3|4|5|7|8][0-9]{9}$/'],
            'password' => ['bail','required','regex:/^[a-z0-9A-Z]{6,16}$/'],
        ];
    }

    /**
     * 获取已定义验证规则的错误消息。
     *
     * @return array
     */
    public function messages()
    {
        return [
            'tel.required' => '请输入手机号',
            'tel.regex' => '您输入的手机号格式不正确',
            'password.required'  => '请输入密码',
            'password.regex' => '您输入的密码格式不正确',
        ];
    }

    /**
     * {@inheritdoc}
     */
    protected function formatErrors(Validator $validator)
    {
        return ApiResult::failure( $validator->errors()->first() )->getResult();
    }
}
